package the.formula.calculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class Trigonometry extends Activity {
	
	private RadioGroup g;
	private TableLayout tl;
    private TextView[] textViewArray = new TextView[6];
    private String sAngle;
    private double angleNum;
    private double[] answer = new double[6];
    private EditText angle;
    private boolean answerAdded = false;
    private LinearLayout ll;
    private LinearLayout ll2;
	

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.trigonometry_activity);
        g = (RadioGroup)findViewById(R.id.trigRadio);
        tl = (TableLayout)findViewById(R.id.trigLayout);
        ll = (LinearLayout)findViewById(R.id.trigLL);
        ll2 = (LinearLayout)findViewById(R.id.trigLL2);
        for(int i = 0; i < textViewArray.length; i++) {
        	textViewArray[i] = new TextView(this);
        }
    }
        
    public void onClick(View v) {
    	switch(v.getId()) {
    		case R.id.trigSolve:
    			getAngles();
    	}
    }

    private void getAngles() {
    	angle = (EditText)findViewById(R.id.angle_value);
    	sAngle = angle.getText().toString();
    	angleNum = Double.parseDouble(sAngle);
		switch(g.getCheckedRadioButtonId()) {
			case R.id.degrees:
				angleNum = angleNum * Math.PI / 180.0;
				break;
			case R.id.radians:
				break;
		}
		
		answer[0] = sine(angleNum);
		answer[1] = cosine(angleNum);
		answer[2] = tangent(angleNum);
		answer[3] = secant(angleNum);
		answer[4] = cosecant(angleNum);
		answer[5] = cotangent(angleNum);
		textViewArray[0].setText("Sine: " + answer[0]);
		textViewArray[1].setText("Cosine: " + answer[1]);
		textViewArray[2].setText("Tangent: " + answer[2]);
		textViewArray[3].setText("Secant: " + answer[3]);
		textViewArray[4].setText("Cosecant: " + answer[4]);
		textViewArray[5].setText("Cotangent: " + answer[5]);
		
		if(answerAdded) {
			ll.removeView(textViewArray[0]);
			ll.removeView(textViewArray[1]);
			ll.removeView(textViewArray[2]);
			ll2.removeView(textViewArray[3]);
			ll2.removeView(textViewArray[4]);
			ll2.removeView(textViewArray[5]);
		}
		
		ll.addView(textViewArray[0]);
		ll.addView(textViewArray[1]);
		ll.addView(textViewArray[2]);
		ll2.addView(textViewArray[3]);
		ll2.addView(textViewArray[4]);
		ll2.addView(textViewArray[5]);
		answerAdded = true;
	}

	private double sine(double x) {
		double result;
    	if(Math.sin(x) < .000000001 && Math.sin(x) > 0) {
    		result = Double.NaN;
    	} else {
        	result = Math.sin(x);
        	if(result < .0000000001 && result > 0) {
        		result = 0;
        	}
    	}
        return result;
    }
    
    private double cosine(double x) {
        double result = Math.cos(x);
    	if(result < .0000000001 && result > 0) {
    		result = 0;
    	}
        return result;
    }
    
    private double tangent(double x) {
    	double result;
    	if(Math.cos(x) < .000000001 && Math.cos(x) > 0) {
    		result = Double.NaN;
    	} else {
	        result = Math.tan(x);
	    	if(result < .0000000001 && result > 0) {
	    		result = 0;
	    	}
    	}
        return result;
    }
    
    private double secant(double x) {
    	double result;
    	if(Math.sin(x) < .000000001 && Math.sin(x) > 0) {
    		result = Double.NaN;
    	} else {
    		result = 1.0 / Math.sin(x);
	    	if(result < .0000000001 && result > 0) {
	    		result = 0;
	    	}
    	}
        return result;
    }
    
    private double cosecant(double x) {
    	double result;
    	if(Math.cos(x) < .000000001 && Math.cos(x) > 0) {
    		result = Double.NaN;
    	} else {
	    	result = 1.0 / Math.cos(x);
	    	if(result < .0000000001 && result > 0) {
	    		result = 0;
	    	}
    	}
        return result;
    }
    
    private double cotangent(double x) {
        double result = 1.0 / Math.tan(x);
    	if(result < .0000000001 && result > 0) {
    		result = 0;
    	}
        return result;
    }
}